package com.account_main.controller;


import com.account_main.config.BakingConfig;
import com.account_main.service.UserService;
import com.account_protocol.pojo.dto.UserDTO;
import com.account_protocol.pojo.vo.SelectByAccountVO;
import com.common.result.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.common.result.ResultEnum;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("v1/user/")
@Api("UserController")
@Validated
@CrossOrigin
//@DependsOn("BakingConfig")
public class UserController {
//    @Value("${baking.haha}")
//    @Value("${baking.test}")
//    private String a;
    @Value("${config.test}")
    private String a;
    @Value("${myconfig.haha}")
    private String b;
//    @Value("${config.test114}")
//    private String c;


    public UserController(){
        log.info("aaaaaaaaaaa啊啊啊啊啊啊啊啊");
        log.info(BakingConfig.test);
        log.info("測試依賴注入：{}",a);

    }
    @Autowired
    private UserService userService;
//    public UserController(){
//        log.info("创建userController");
//    }

    /*
    @Param
    account 账号
    password 密码
    nickname 昵称
     */


    @PostMapping("add")
    @ApiOperation("添加用户")
    public JsonResult addUser(@Valid @RequestBody UserDTO userDTO){
        log.info("开始添加用户");
        String account=userDTO.getAccount();
        SelectByAccountVO selectByAccountVO = userService.selectNicknameByAccount(account);
        if (selectByAccountVO!=null){
            return new JsonResult(ResultEnum.USER_ACCOUNT_HAVE_EXIST);
        }else {
            String nickname=userDTO.getNickname();
            log.info("UserController执行Service方法");
            JsonResult result=userService.addUser(userDTO);
            return result;
        }
    }
    @PostMapping("update/userInfo/profilePicture")
    public JsonResult updateUserProfilePicture(@RequestParam MultipartFile multipartFile,SecurityContextHolder securityContextHolder){
        log.info("accountController开始处理用户上传头像请求");
        return userService.updateUserProfilePicture(multipartFile,securityContextHolder);

    }

//    @ApiOperation("测试添加用户")
//    @PostMapping("/testAdd")
//    public JsonResult testAddUser(@RequestBody UserDTO userDTO) {
//        try {
//            int a = userService.addUser(userDTO);
//            if (a != 0) {
//                return new JsonResult().ok();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    @ApiOperation("测试查询账户")
    @PostMapping("/selectByAccount")
    public String selectByAccount(@RequestParam String account){
        SelectByAccountVO selectByAccountVO =userService.selectNicknameByAccount(account);
        log.info("我是vo"+selectByAccountVO);
        if (selectByAccountVO!=null){
            return selectByAccountVO.getNickname();
        }else {
            return "我草泥马又失败了";
        }
    }
    @PostMapping("test")
    public String test(){
        log.info("測試依賴注入{}",a);
        log.info("測試依賴注入{}",b);
//        log.info("測試依賴注入{}",c);
        return "测试成功";
    }
}
